package com.zonta.finance.serivce.impl;


import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.CreateAssistCalculateCateForm;
import com.zonta.finance.domain.form.DelAssistCalculateCateForm;
import com.zonta.finance.domain.po.AssistCalculateCate;
import com.zonta.finance.mapper.AssistCalculateCateMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.serivce.IAssistCalculateCateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 辅助核算类别 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-30
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AssistCalculateCateServiceImpl extends ServiceImpl<AssistCalculateCateMapper, AssistCalculateCate> implements IAssistCalculateCateService {
    private final TokenService<AdminDTO> tokenService;
    /**
     * 创建辅助核算类别
     * @param form
     * @return
     */
    @Override
    public Boolean createByFrom(CreateAssistCalculateCateForm form) {

        return null;
    }

    /**
     * 删除辅助核算类别
     * @param form
     * @return
     */
    @Override
    public Boolean delById(DelAssistCalculateCateForm form) {
        AssistCalculateCate assistCalculateCate = getById2(form.getId());
        if (ObjUtil.isNotEmpty(assistCalculateCate)){
            throw new BizException("辅助核算类别不存在");
        }
        if (assistCalculateCate.getUseCount() > 0) {
            throw new BizException("该辅助核算下面已经有辅助核算数据不能删除");
        }
        return null;
    }

    /**
     * 查询辅助核算类别信息
     *
     * @param id
     * @return
     */
    public AssistCalculateCate getById2(long id) {
        LambdaQueryWrapper<AssistCalculateCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(AssistCalculateCate::getId,AssistCalculateCate::getName,AssistCalculateCate::getCustomerColumn,AssistCalculateCate::getLevel,AssistCalculateCate::getCode,AssistCalculateCate::getUseCount)
                .eq(AssistCalculateCate::getId,id)
                .eq(AssistCalculateCate::getTenantId,tokenService.getThreadLocalTenantId());

        return getOne(wrapper);
    }
}
